#include <stdio.h>
#include <kewinternal.h>

#define SOURCE_FILE_NAME "Variable"

#define value(OBJECT) ((OBJECT)->context[0])


METHOD(Deprecated_Assign)
{
  value(receiver) = args[0];
  results[0] = receiver;
}


METHOD(Assign)
{
  value(receiver) = args[0];
}


METHOD(Read)
{
  results[0] = value(receiver);
}


static KewObjectType var_type = {
  SOURCE_FILE_NAME, __LINE__,
  1,
  {
    {"$", Read, 0, 1},
    {":=", Deprecated_Assign, 1, 1}, /* FIXME: deprecated */
    {":=", Assign, 1, 0},
    {"=", Kew__GenericEqual, 1, 1},
    {NULL, NULL, 0, 0}
  }
};

KewObject *
KewNewVar(KewObject *initial_value)
{
  KewObject *result;

  result = KewNewObject(&var_type);
  value(result) = initial_value;
  return result;
}

METHOD(FactoryDefaultNew)
{
  results[0] = KewNewVar(KewNil);
}

METHOD(FactoryAssignedNew)
{
  results[0] = KewNewVar(args[0]);
}

static KewObjectType var_factory_type = {
  SOURCE_FILE_NAME, __LINE__,
  0,
  {
    {"=", Kew__GenericEqual, 1, 1},
    {"hash", Kew__GenericHash, 0, 1},
    {"new", FactoryDefaultNew, 0, 1},
    {"new", FactoryAssignedNew, 1, 1},
    {NULL, NULL, 0, 0}
  }
};

void
Kew__InitVar(void)
{
  KewVar = KewNewObject(&var_factory_type);
}
